using System;
using System.Collections.Generic;
using NUnit.Framework;
using RMH.Entities;
using RMH.Services;
using RMH.Web.Controllers;
using RMH.Web.Models.Binders;

namespace RMH.UnitTests.Web.Binders
{
    [TestFixture]
    public class PatientBinder_when_multiple_support_people_on_the_the_form: base_binding_test<PatientController>
    {
        private int patientId = 23;

        SupportPerson person1 = new SupportPerson { Id = 1 };
        SupportPerson person2 = new SupportPerson { Id = 2 };
        Patient patient = new Patient();
        private Patient returnedPatient;


        public override void establish_context()
        {
            patient.Relationships.Add(new Relationship { Id = 3, SupportPerson = person1 });
            patient.Relationships.Add(new Relationship { Id = 4, SupportPerson = new SupportPerson { Id = 3 } });
            form.Add("Id", patientId.ToString());

            form.Add("SupportPerson", "1,2");
            form.Add("RelationShipType", "5,6");

            Mock<IPatientService>()
                .Setup(s => s.Get(23))
                .Returns(patient);

            Mock<ISupportPersonService>()
                .Setup(s => s.Get(1))
                .Returns(person1);

            Mock<ISupportPersonService>()
                .Setup(s => s.Get(2))
                .Returns(person2);

            context.HttpRequest.Setup(r => r.Form).Returns(form);  
        }
        public override void because()
        {
            var binder = Create<PatientBinder>();
            returnedPatient = (Patient)getBindingResult(binder);
        }

        [Test]
        public void person_should_have_all_relationships_in_form()
        {
            List<Relationship> relationships = new List<Relationship>(returnedPatient.Relationships);

            relationships.FindAll(r => r.SupportPerson.Id == 1).Count.ShouldEqual(1);               
            relationships.FindAll(r=>r.SupportPerson.Id == 2).Count.ShouldEqual(1);
                                
        }

        [Test]
        public void removed_person_should_not_be_in_relationships()
        {
            List<Relationship> relationships = new List<Relationship>(returnedPatient.Relationships);
            relationships.FindAll(r => r.SupportPerson.Id == 3).Count.ShouldEqual(0);
        }

        [Test]
        public void relationships_should_be_of_the_right_type()
        {
            List<Relationship> relationships = new List<Relationship>(returnedPatient.Relationships);

            relationships.FindAll(r => r.SupportPerson.Id == 1 && r.Type == (RelationshipType)5).Count.ShouldEqual(1);
            relationships.FindAll(r => r.SupportPerson.Id == 2 && r.Type == (RelationshipType)6).Count.ShouldEqual(1);
        }
    }
}